#include "InputLayer.h"
#include "MainLayer.h"
#include "SpriteAnimation.h"
#include "EffectFactory.h"
#include "MathUtil.h"
#include "TestGameScene.h"


InputLayer::InputLayer(void)
{
	_particle = NULL;

	_playerHealthBar = NULL;
	_stateBar = NULL;
	_speedrunBar = NULL;
	_lblScore = NULL;
	_lblStar = NULL;
	_lblKm = NULL;
	_butPause = NULL;
}


InputLayer::~InputLayer(void)
{
	CC_SAFE_RELEASE(_particle);

	CC_SAFE_RELEASE(_playerHealthBar);
	CC_SAFE_RELEASE(_stateBar);
	CC_SAFE_RELEASE(_speedrunBar);
	CC_SAFE_RELEASE(_lblScore);
	CC_SAFE_RELEASE(_lblStar);
	CC_SAFE_RELEASE(_lblKm);
	CC_SAFE_RELEASE(_butPause);
}

void InputLayer::ccTouchesMoved( CCSet *pTouches, CCEvent *pEvent )
{
	CCTouch* touch = dynamic_cast<CCTouch*>(*pTouches->begin());
	CCNode* a = this->getParent()->getChildByTag(1);
	PlayerPlane* plant = dynamic_cast<MainLayer*>(getParent()->getChildByTag(1))->getMyPlant();
	CCPoint currentPos = plant->getPosition();
	plant->setPositionX(currentPos.x + touch->getDelta().x);
	plant->setPositionY(currentPos.y + touch->getDelta().y);
	
}

bool InputLayer::init()
{
	if (!CCLayer::init())
		return false;
	setTouchEnabled(true);

	CCMenuItemImage* upButton = CCMenuItemImage::create("up.png", "up.png", this, SEL_MenuHandler(&InputLayer::upClick));
	upButton->setPosition(ccp(10, 400));
	CCMenuItemImage* downButton = CCMenuItemImage::create("down.png", "down.png", this, SEL_MenuHandler(&InputLayer::downClick));
	downButton->setPosition(ccp(10, 100));
	downButton->setScale(2);
	upButton->setScale(2);

	CCMenu* menu = CCMenu::create(upButton, downButton, NULL);
	menu->setPosition(ccp(0, 0));
	menu->setAnchorPoint(CCPointZero);
	addChild(menu);

	PlayerHealthBar* bar = PlayerHealthBar::create();
	CCSize screenSize = CCEGLView::sharedOpenGLView()->getDesignResolutionSize();
	bar->setAnchorPoint(ccp(0, 1));
	bar->setPosition(ccp(0, screenSize.height - 50));
	addChild(bar, 1);
	setPlayerHealthBar(bar);
	

	// Init stateBar
	StateBar* stateBar = StateBar::create();
	stateBar->setPosition(ccp(screenSize.width - 100, screenSize.height - 300));
	stateBar->setAnchorPoint(ccp(0, 0));
	stateBar->setContentSize(CCSizeMake(10, 10));
	setStateBar(stateBar);
	addChild(stateBar, 1);

	// Speedrun Bar
	Speedrun_Bar* speedBar = Speedrun_Bar::create();
	speedBar->setPosition(ccp(5, 950));
	speedBar->setVisible(false);
	addChild(speedBar, 1);
	setSpeedrunBar(speedBar);

	// Lable score
	CCLabelTTF* lbl = CCLabelTTF::create("0", "Arial", 50, CCSizeMake(200, 80), kCCTextAlignmentRight, kCCVerticalTextAlignmentCenter);
	lbl->setAnchorPoint(ccp(1, 0));
	lbl->setPosition(ccp(700, 1014));
	addChild(lbl, 1);
	setLblScore(lbl);

	// Lable star
	lbl = CCLabelTTF::create("0", "Arial", 30, CCSizeMake(200, 80), kCCTextAlignmentRight, kCCVerticalTextAlignmentCenter);
	lbl->setAnchorPoint(ccp(1, 0));
	lbl->setPosition(ccp(700, 965));
	addChild(lbl, 1);
	setLblStar(lbl);

	// Lable km
	lbl = CCLabelTTF::create("0", "Arial", 30, CCSizeMake(200, 80), kCCTextAlignmentRight, kCCVerticalTextAlignmentCenter);
	lbl->setAnchorPoint(ccp(1, 0));
	lbl->setPosition(ccp(700, 928));
	addChild(lbl, 1);
	setLblKm(lbl);

	// Menu
	// Pause button
	CCMenuItemImage* pauseButton = CCMenuItemImage::create("img_pause.png", "img_pause.png", "img_pause.png", this, SEL_MenuHandler(&InputLayer::callbackPauseClick));
	pauseButton->setAnchorPoint(CCPointZero);
	pauseButton->setPosition(ccp(477, 1016));
	menu->addChild(pauseButton);
	setPauseButton(pauseButton);


// 		
// 		JniMethodInfo minfo;
// 
// 		if(JniHelper::getStaticMethodInfo(minfo, 
// 			"com/CGKStudio/plant/CGKPlant", 
// 			"openURL", 
// 			"(Ljava/lang/String;)V"))
// 		{
// 			//string pszUrl=;
// 			CCLog("OK-- IT on");
// 			jstring StringArg1 = minfo.env->NewStringUTF("https://www.google.com.vn/");
// 			minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, StringArg1);
// 			minfo.env->DeleteLocalRef(StringArg1);
// 			minfo.env->DeleteLocalRef(minfo.classID);
// 		}


	return true;
}

void InputLayer::upClick( CCObject* sender )
{
	PlayerPlane* plant = dynamic_cast<MainLayer*>(getParent()->getChildByTag(1))->getMyPlant();
	int currentLvl = plant->getWeaponLevel();
	currentLvl++;
	
	plant->setWeaponLevel(currentLvl);
}

void InputLayer::downClick( CCObject* sender )
{
	PlayerPlane* plant = dynamic_cast<MainLayer*>(getParent()->getChildByTag(1))->getMyPlant();
	int currentLvl = plant->getWeaponLevel();
	currentLvl--;

	plant->setWeaponLevel(currentLvl);
}


MainLayer* InputLayer::getMainLayer()
{
	return dynamic_cast<MainLayer*>(getParent()->getChildByTag(MAINLAYER_TAG));
}

void InputLayer::onEnter()
{
	CCLayer::onEnter();
	getMainLayer()->getMyPlant()->setHealthBar(_playerHealthBar);
}



void InputLayer::callbackPauseClick( CCObject* sender )
{
	if (getMainLayer()->getIsPause())
		getMainLayer()->resumeGame();
	else
		getMainLayer()->pauseGame();
}
